let arr = [1, 2, 3, 4, 5, 6]

Array.prototype.my_reduce = function (cb, ...args) {
  let res, index = 0
  if (args.length) {
    res = args[0]
  } else {
    res = this[0]
    index = 1
  }


  for (let i = index; i < this.length; i++) {
    res = cb(res, this[i], i, this)
  }
  return res
}

// return 的值赋回给 pre，当数组遍历结束，返回最终的 pre 的值
const res = arr.my_reduce((pre, item, index, arr) => {
  // console.log(pre, item, index, arr);

  return pre + item
})

console.log(res);
